package com.fitness.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.google.gson.Gson;

import okhttp3.FormBody;
import okhttp3.FormBody.Builder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
@SuppressWarnings("all")
public class HttpUtil {
	final static int GET = 1;
	final static int POST = 2;
	/**
	 * 发起一个get请求
	 * @param url
	 * @return
	 */
	public static Map<String,String> goURL(String url){
		
		return  goURL(url,GET,null);
	}
	/**
	 * 发起一个http请求
	 * @param url url
	 * @param method 请求方法
	 * @param params 请求参数
	 * @return
	 */
	public static Map<String, String> goURL(String url,int method,Map<String,Object> params) {
		OkHttpClient oc = new OkHttpClient();
		Request request  = null;
		Map<String,String> map = null;
		//处理参数
		if(params!=null) {
			switch(method) {
			case GET:
				request = new Request.Builder().url(paramGet(url, params)).build();
				break;
			case POST:
				 request = new Request.Builder().url(url).post(paramPost(params)).build();
				break;
			}
		}else 
			request = new Request.Builder().url(url).build();
		//执行请求 获取响应数据
	  	try {
	  		//这里还要处理 请求成功但是返回的是404 或者是500
			Response response =  oc.newCall(request).execute();
			if(response.isSuccessful()){
				Gson gson = new Gson();
				map =  gson.fromJson(response.body().string(), HashMap.class);
				map.put("request_status","true");
			}else{
				map = new HashMap<>();
				map.put("request_status","false");
			} 
			
		} catch (IOException e) {
			//如果请求失败 那么是服务器网络异常
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * get参数的处理类
	 * @param url
	 * @param params
	 * @return
	 */
	private static String paramGet(String url,Map<String,Object> params){
		//如果没有参数 那么直接返回
		Iterator<String> iter =params.keySet().iterator();
		if(!iter.hasNext()) return url;
		//如果有参数 那么遍历参数 并且最后一个参数不加&
		url+="?";
		for(;iter.hasNext();){
			String name = iter.next();//获取参数的名称
			Object value = params.get(name);//参数的值
			//判断值是否是最后一个
			url+=iter.hasNext()?(name+"="+value+"&"):(name+"="+value);
		}
		return url;
	}
	/**
	 * post参数的处理类
	 * @param params
	 * @return
	 */
	private static FormBody paramPost(Map<String,Object> params){
		//如果参数列表为空 那么直接返回
		Builder builder = new Builder();
		Iterator<String> iter =params.keySet().iterator();
		if(!iter.hasNext()) return builder.build();
		//解析参数 循环添加
		for(;iter.hasNext();){
			String name = iter.next();//获取参数的名称
			String value = params.get(name).toString();//参数的值
			builder.add(name, value);
		}
		return builder.build();
	}
	
	
	
	
	public static void main(String[] args) throws IOException {
		String url = "http://yuntuapi.amap.com/datasearch/around?tableid=598ab2307bbf190cbd30e507"
				+ "&center=114.3543,30.53648&radius=5000&limit=10&page=1&key=f511e0a76e2f7cf969a7f7311c1fefc9";
		String url1 = "http://39.108.14.99/carehome/info/article/list?page=1&pageSize=10";
		OkHttpClient oc = new OkHttpClient();
		Request request = new Request.Builder().url(url1).build();
		Response response = oc.newCall(request).execute();
		if(response.isSuccessful()){
			System.out.println("成功");
			ResponseBody body =  response.body();
			BufferedReader br = new BufferedReader(body.charStream());
			int len = 0;
			char[] flush = new char[1024];
			while((len =br.read(flush))!=-1){//flush 缓冲数组会重新放入 但是 如果第一次放入 1024 第二次放入512 那么后面的512 任然存在
				System.out.print(new String(flush,0,len));
			}
		}else{
			throw new IOException("错误了"+response);
		}

	}
}
